package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.common.PageResult;
import com.oracle.mapper.BjMapper;
import com.oracle.pojo.Bj;
import com.oracle.pojo.BjExample;
import com.oracle.service.BjService;
import com.oracle.vo.BjVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class BjServiceImpl implements BjService {
    @Autowired(required = false)
    private BjMapper bjMapper;

    @Override
    public List<Bj> findList()
    {
        List<Bj> bjs = bjMapper.selectByExample(null);
        return bjs;
    }

    @Override
    public PageResult findPages(Integer pageNo,Integer pageSize,Bj bj)
    {
        PageHelper.startPage(pageNo,pageSize);

        BjExample example = new BjExample();
        BjExample.Criteria criteria = example.createCriteria();

        if (!ObjectUtils.isEmpty(bj.getBjId()))
        {
            criteria.andBjIdEqualTo(bj.getBjId());
        }
        if (!ObjectUtils.isEmpty(bj.getBjZyid()))
        {
            criteria.andBjZyidEqualTo(bj.getBjZyid());
        }
        if (!ObjectUtils.isEmpty(bj.getBjZyname()))
        {
            criteria.andBjZynameEqualTo(bj.getBjZyname());
        }
        if (!ObjectUtils.isEmpty(bj.getBjBzy()))
        {
            criteria.andBjBzyEqualTo(bj.getBjBzy());
        }

        example.setOrderByClause("bj_id");

        Page<Bj> bjPage = (Page<Bj>)bjMapper.selectByExample(example);

        List<Bj> bjList = bjPage.getResult();

        List<BjVo> bjVoList = bulidBjVoList(bjList);

        return new PageResult(bjPage.getTotal(),bjVoList);
    }

    private List<BjVo> bulidBjVoList(List<Bj> bjList)
    {
        List<BjVo> bjVoList = new ArrayList<>();
        bjList.forEach(bj -> {
            BjVo bjVo2 = new BjVo();
            BeanUtils.copyProperties(bj,bjVo2);
            bjVoList.add(bjVo2);
        });
        return bjVoList;
    }
}
